﻿using System;
using System.Collections;
using System.Collections.Generic;
using Framework;
using GameProtocols;
using TrueSync;
using TrueSync.Physics2D;

public class FightGame : Singleton<FightGame>, IGame, IGameStateMachine
{
    public ObjHandle<LogicEntity> myPlayer = ObjHandle<LogicEntity>.NULL;
    public ObjHandle<LogicEntity> otherPlayer = ObjHandle<LogicEntity>.NULL;

    IGameState mCurrentGameState;

    public CollisionWorld CollisionWorld;
    public FP MinSceneX, MaxSceneX;

    public void Init(params object[] gameParam)
    {
        //physics
        IBroadphase dynamicTreeBroadPhase = new DynamicTreeBroadPhase();
        CollisionWorld = new CollisionWorld(dynamicTreeBroadPhase);
        //场景边界
        MinSceneX = -10; MaxSceneX = 10;

        ChangeState(new FightGameInitState());
    }

    public void onDestroy()
    {
        CollisionWorld = null;
    }

    public void Update(PKG_FRAME frame)
    {
        if (mCurrentGameState != null)
            mCurrentGameState.Execute(frame); 
    }

    public IGameState GetCurrentState()
    {
        return mCurrentGameState;
    }

    public void ChangeState(IGameState nextState)
    {
        if (mCurrentGameState != null)
            mCurrentGameState.Exit();
        mCurrentGameState = nextState;
        mCurrentGameState.Enter(this);
    }

    public ObjHandle<LogicEntity> GetMyEntity(long playerID)
    {
        if (myPlayer.IsValid && myPlayer.handle.PlayerID == playerID)
            return myPlayer;
        else if (otherPlayer.IsValid && otherPlayer.handle.PlayerID == playerID)
            return otherPlayer;
        else
            return ObjHandle<LogicEntity>.NULL;
    }

    public ObjHandle<LogicEntity> GetRivalEntity(long playerID)
    {
        if (myPlayer.IsValid && myPlayer.handle.PlayerID == playerID)
            return otherPlayer;
        else if (otherPlayer.IsValid && otherPlayer.handle.PlayerID == playerID)
            return myPlayer;
        else
            return ObjHandle<LogicEntity>.NULL;
    }
}
